﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Author: toanvt
/// CreatedDate: 13/12/2011
/// Description: Danh mục tin tức Provider
/// </summary>
namespace EHTWebsite.Code
{
    public class DanhMucTinTucProvider
    {
        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 13/12/2011
        /// Description: Insert, Update Danh mục tin tức
        /// </summary>
        /// <param name="theDanhMucTinTuc">Đối tượng Danh mục</param>
        /// <param name="IsAdd">trang thái thêm mới hay không</param>
        public static Int32 IUDanhMucTinTuc(DanhMucTinTuc theDanhMucTinTuc, bool isAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUDanhMucTinTuc";
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter theDanhMucTinTucIDParameter = new SqlParameter();

            theDanhMucTinTucIDParameter.ParameterName = "@DanhMucTinTucIDOut";
            theDanhMucTinTucIDParameter.Direction = ParameterDirection.Output;
            theDanhMucTinTucIDParameter.DbType = DbType.Int32;

            command.Parameters.Add(theDanhMucTinTucIDParameter);
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", theDanhMucTinTuc.DanhMucTinTucID));
            command.Parameters.Add(new SqlParameter("@TenDanhMucTinTucVN", theDanhMucTinTuc.TenDanhMucTinTucVN));
            command.Parameters.Add(new SqlParameter("@TenDanhMucTinTucOther", theDanhMucTinTuc.TenDanhMucTinTucOther));
            command.Parameters.Add(new SqlParameter("@ChuyenMucChaID", theDanhMucTinTuc.ChuyenMucChaID));
            command.Parameters.Add(new SqlParameter("@SapXep", theDanhMucTinTuc.SapXep));
            command.Parameters.Add(new SqlParameter("@NhomDanhMucID", theDanhMucTinTuc.NhomDanhMucID));
            command.Parameters.Add(new SqlParameter("@Trangthai", theDanhMucTinTuc.Trangthai));
            command.Parameters.Add(new SqlParameter("@Icon", theDanhMucTinTuc.Icon));
            command.Parameters.Add(new SqlParameter("@IsAdd", isAdd));

            DataProvider.ExecuteNoneQuery(command);

            return Int32.Parse(command.Parameters["@DanhMucTinTucIDOut"].Value.ToString());
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 13/12/2011
        /// Description: Xóa chuyên mục theo mã chuyên mục
        /// </summary>
        /// <param name="danhMucTinTucID">Mã chuyên mục</param>
        public static void DeleteDanhMucTinTucByDanhMucID(Int32 danhMucTinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteDanhMucTinTucByDanhMucTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", danhMucTinTucID));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 13/12/2011
        /// Description: Lấy danh sách chuyên mục tin tức
        /// </summary>
        /// <returns>Danh sách chuyên mục</returns>
        public static DataSet GetDanhMucTinTucsByNhomDanhMucID(Int32 DanhMucTinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucTinTucsByNhomDanhMucID";
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", DanhMucTinTucID));
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }

        /// <summary>
        /// Author: chiennv
        /// CreatedDate: 4/2/2012
        /// Description: Lấy danh sách chuyên mục tin tức theo nhom danh muc có phân trang
        /// </summary>
        /// <param name="DanhMucTinTucID"></param>
        /// <returns></returns>
        public static DataSet GetDanhMucTinTucsByNhomDanhMucIDwithPaging(Int32 nhomDanhMucID, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucTinTucsByNhomDanhMucIDwithPaging";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", nhomDanhMucID));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;


        }
       

        /// <summary>
        /// lay danh sach tin tuc cho backend
        /// author: chiennv
        /// create date: 6/2/2012
        /// </summary>
        /// <param name="DanhMucTinTucID"></param>
        /// <returns></returns>
        public static DataSet GetDanhsachtintucForBackEnd()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhsachtintucForBackEnd";
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }

        /// <summary>
        /// Update danh muc tin tuc boi numberorder
        /// author: chiennv
        /// create: 08/02/2012
        /// </summary>
        /// <param name="DanhMucTinTucID"></param>
        /// <returns></returns>
        public static void UpdateNumberOfOrderByDanhMucTinTucID(Int32 DanhMucTinTucID, Int32 SapXep)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UpdateNumberOfOrderByDanhMucTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", DanhMucTinTucID));
            command.Parameters.Add(new SqlParameter("@SapXep", SapXep));
            DataProvider.ExecuteNoneQuery(command);
        }

            public static void ChangePublishBydanhMucTinTucID(int DanhMucTinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_ChangePublishByDanhMucTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", DanhMucTinTucID));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static DataSet GetTinTucsByDanhMucTinTucID(Int32 DanhMucTinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetTinTucByDanhMucTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", DanhMucTinTucID));

            return DataProvider.ExecuteDataSet(command);
        }

        public static DataSet GetDanhMucChaTinTucsByDanhMucTinTucID(Int32 DanhMucTinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucChaTinTucsByDanhMucTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", DanhMucTinTucID));

            return DataProvider.ExecuteDataSet(command);
        }

        public static void UpdateLuotTruyCap(Int32 DanhMucTinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UDanhMucTinTuc_LuotTruyCap";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", DanhMucTinTucID));

            DataProvider.ExecuteNoneQuery(command);
        }

        


        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 13/12/2011
        /// Description: Lấy chuyên mục theo mã chuyên mục
        /// </summary>
        /// <param name="danhMucTinTucID">Mã chuyên mục</param>
        /// <returns>Đối tượng chuyên muc</returns>
        public static DanhMucTinTuc GetDanhMucTinTucByDanhMucID(Int32 danhMucTinTucID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDanhMucTinTucByDanhMucTinTucID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DanhMucTinTucID", danhMucTinTucID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            DanhMucTinTuc theDanhMucTinTuc = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theDanhMucTinTuc = new DanhMucTinTuc();
                theDanhMucTinTuc.DeletedFlag = Boolean.Parse(ds.Tables[0].Rows[0]["DeletedFlag"].ToString());
                theDanhMucTinTuc.TenDanhMucTinTucVN = ds.Tables[0].Rows[0]["TenDanhMucTinTucVN"].ToString();
                theDanhMucTinTuc.TenDanhMucTinTucOther = ds.Tables[0].Rows[0]["TenDanhMucTinTucOther"].ToString();
                theDanhMucTinTuc.ChuyenMucChaID = Int32.Parse(ds.Tables[0].Rows[0]["ChuyenMucChaID"].ToString());
                theDanhMucTinTuc.TinTucHotID = Int32.Parse(ds.Tables[0].Rows[0]["TinTucHotID"].ToString());
                theDanhMucTinTuc.LuotTruyCap = Int32.Parse(ds.Tables[0].Rows[0]["LuotTruyCap"].ToString());
                theDanhMucTinTuc.SapXep = Int32.Parse(ds.Tables[0].Rows[0]["SapXep"].ToString());
                theDanhMucTinTuc.DanhMucTinTucID = Int32.Parse(ds.Tables[0].Rows[0]["DanhMucTinTucID"].ToString());
                theDanhMucTinTuc.Icon = ds.Tables[0].Rows[0]["Icon"].ToString();
                theDanhMucTinTuc.Trangthai = Boolean.Parse(ds.Tables[0].Rows[0]["Trangthai"].ToString());
                theDanhMucTinTuc.NhomDanhMucID = Int32.Parse(ds.Tables[0].Rows[0]["NhomDanhMucID"].ToString());
            }

            return theDanhMucTinTuc;
        }

        
    }
}